package zencstudio.sgrealtor.view.phone;

import zencstudio.sgrealtor.R;
import zencstudio.sgrealtor.model.PropertyDetail;
import zencstudio.sgrealtor.service.IWireFrameService;
import zencstudio.sgrealtor.service.WireFrameServiceFactory;
import zencstudio.sgrealtor.utils.Const;
import zencstudio.sgrealtor.utils.WFErrorCode;
import zencstudio.sgrealtor.utils.WFUtils;
import zencstudio.sgrealtor.view.common.BaseActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

public class PropertyDetailActivity extends BaseActivity{
	private TextView txtName;
	private ImageView imgView;
	private TextView txtPrice;
	private Button btnMoreImg;
	private Button btnShowLocation;
	private Button btnContactUs;
	private PropertyDetail property;
	private TextView txtPropertyName;
	private TextView txtPropertyType;
	private TextView txtTenure;
	private TextView txtBedrooms;
	private TextView txtBathrooms;
	private TextView txtLocation;
	private TextView txtDescription;
	
	private int id =0;
	@Override
	protected void initUI() {
		setContentView(R.layout.property_detail);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	
		 if(!WFUtils.checkInternetConnection(this)){
	        	showNetworkErrorDlg();
 			return;
 		}	
		 TextView title = (TextView) findViewById(R.id.txtTitle);
			title.setText(getString(R.string.infor));
		txtName = (TextView) findViewById(R.id.txtName);
		imgView = (ImageView) findViewById(R.id.imgProperty);
		txtPrice = (TextView) findViewById(R.id.txtPrice);
		btnMoreImg = (Button) findViewById(R.id.btnMoreImg);
		btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
		btnContactUs = (Button) findViewById(R.id.btnContact);
		txtPropertyName = (TextView) findViewById(R.id.txtPropertyName);
		txtPropertyType = (TextView) findViewById(R.id.txtPropertyType);
		txtTenure = (TextView) findViewById(R.id.txtTenure);
		txtBedrooms = (TextView) findViewById(R.id.txtBedrooms);
		txtBathrooms = (TextView) findViewById(R.id.txtBathrooms);
		txtLocation = (TextView) findViewById(R.id.txtLocation);
		txtDescription = (TextView)findViewById(R.id.txtDescription);
		
		setControls();
		loadData();
	}
	private void loadData(){
		showProgressDlg();
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				id = getIntent().getIntExtra(Const.PROPERTYID,0);
				IWireFrameService service = WireFrameServiceFactory.getInstance().getService();
				Message msg = handler.obtainMessage();
				try {
					property = service.getProperty(id);
			
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					msg.arg1 = WFErrorCode.NETWORK_ERROR.getValue();
				}
				msg.obj = property;
				handler.sendMessage(msg);
			}
		});
		thread.start();
	}
	private Handler handler = new Handler(){
		public void handleMessage (Message msg){
			dismissProgressDlg();
			if(msg.arg1==WFErrorCode.NETWORK_ERROR.getValue()){
				showNetworkErrorDlg();
				return;
			}
		
			PropertyDetail propertyDetail=(PropertyDetail) msg.obj;
			txtName.setText(propertyDetail.getName().toString());
			txtPrice.setText(propertyDetail.getPriceFormat().toString());
			try {
				Picasso.with(PropertyDetailActivity.this).load(propertyDetail.getImgThumbnailUrl()).fit().centerInside().error(R.drawable.noimage).placeholder(R.drawable.noimage).into(imgView);
			} catch(Exception e){
				e.printStackTrace();
			}
			txtPropertyName.setText(propertyDetail.getName().toString());
			txtPropertyType.setText(propertyDetail.getPropertyType().toString());
			txtTenure.setText(propertyDetail.getTenure().toString());
			txtBedrooms.setText(propertyDetail.getBedrooms().toString());
			txtBathrooms.setText(propertyDetail.getBathrooms().toString());
			txtLocation.setText(propertyDetail.getLocation().toString());
			txtDescription.setText(Html.fromHtml(propertyDetail.getDescription()));
			if(WFUtils.removeNull(propertyDetail.getImgGalleryUrl()).length<=0){
				btnMoreImg.setEnabled(false);
				btnMoreImg.setBackgroundColor(getResources().getColor(R.color.button_disable));
			}
		}
	};
	private void setControls(){
		
		btnShowLocation.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(PropertyDetailActivity.this,PropertyMap.class);
				intent.putExtra(Const.PROPERTYID, property);
				startActivity(intent);
			}
		});
		btnContactUs.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(PropertyDetailActivity.this,ContactActivity.class);
				startActivity(intent);
			}
		});
		btnMoreImg.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(PropertyDetailActivity.this,PropertyImage.class);
				intent.putExtra(Const.PROPERTYID, property);
				startActivity(intent);
			}
		});
	}
}
